﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SQLite;


namespace Medicine.DbContext
{
    public static class DbC
    {
        public readonly static string DbPath = "ormdemo.db";
        private static SQLiteConnection _db;

        private static void InitTables()
        {
            //string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), DbPath);
            //_db = new SQLiteConnection(dbPath, false);
            _db.CreateTable<Models.Medicine>();
            _db.CreateTable<Models.AppSet>();
            //var count = _db.Table<Models.Medicine>().Count();
        }


        private static void SureDbIsActivity()
        {
            if (_db == null)
                InitDb();
        }

        public static void InitDb()
        {
            string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), DbPath);
            _db = new SQLiteConnection(dbPath, false);
            InitTables();
        }

        #region QueryCommand
        public static T FirstQuery<T>(Expression<Func<T, bool>> expression) where T : new()
        {
            SureDbIsActivity();

            var data = _db.Table<T>().Where(expression).FirstOrDefault();
            return data;
        }

        public static List<T> ListQuery<T>(Expression<Func<T, bool>> expression) where T : new()
        {
            SureDbIsActivity();

            var data = _db.Table<T>().Where(expression).ToList();
            return data;
        }

        public static List<T> AllDataQuery<T>() where T : new()
        {
            SureDbIsActivity();

            var data = _db.Table<T>().ToList();
            return data;
        }

        //#region Async
        //public static async List<T> ListQueryAsync<T>(Expression<Func<T, bool>> expression) where T : new()
        //{
        //    SureDbIsActivity();

        //    var data = _db.Table<T>().Where(expression).ToList();
        //    return data;
        //}
        //#endregion
        #endregion

        #region Insert,Updata,Delete
        public static int Insert<T>(T data) where T : new()
        {
            SureDbIsActivity();
            return _db.Insert(data);
        }

        public static int InsertArray<T>(IList<T> data, bool needCheckRepeat = false) where T : Models.BaseModel, new()
        {
            var needData = new List<T>();
            if (needCheckRepeat)
            {
                foreach (var item in data)
                {
                    var hasCount = _db.Table<T>().Where(s => s.Id==item.Id).Count();
                    if (hasCount <= 0)
                        needData.Add(item);
                }
            }
            else
                needData = data.ToList();

            return _db.InsertAll(needData);
        }


        public static int InsertByBaseModel<T>(T data) where T : Models.BaseModel, new()
        {
            SureDbIsActivity();
            data.Id = Guid.NewGuid().ToString().Replace("-", "");
            data.InsertDateTime = DateTime.Now;
            return _db.Insert(data);
        }


        public static int Update<T>(T data) where T : new()
        {
            SureDbIsActivity();
            return _db.Update(data);
        }

        public static int Delete<T>(T data) where T : new()
        {
            SureDbIsActivity();
            return _db.Delete(data);
        }
        #endregion


    }
}